<?php

ini_set('error_reporting', E_ERROR);
// Set the display_errors directive to Off
ini_set('display_errors', 0);
// Log errors to the web server's error log
ini_set('log_errors', 1);
ini_set('html_errors', 0);
ini_set('error_log', 'log/log.txt');
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
    ob_start("ob_gzhandler"); else
    ob_start();
session_cache_expire(10000);
@ini_set('session.gc_maxlifetime', 10000 * 60);
@ini_set("session.name", "auto");
@ini_set("session.cookie_domain", $_SERVER['SERVER_NAME']);
@session_start();



/**
 * set header with cache
 */
//if using Expires header,you have must disable Last-Modified or ETag
//only used both Last-Modified and ETag or both Expires and Cache-Control: max-age
header("Cache-Control: public, max-age=31536000");
header('Last-Modified: ' . gmdate("D, d M Y H:i:s") . ' GMT');
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 31104000) . " GMT");
//header("Pragma: cache");
//header("Content-Encoding: gzip");
require_once 'controllers/_autoLoad.php';
require_once 'config.php';

$url = $_SERVER['REQUEST_URI'];
$url = preg_replace('/^\//', '', $url);

$aryURL = explode('/', $url);

//can sua lai $aryURL[0] khi tach
if ($aryURL[0] != '') {

    $controller = $aryURL[0];

    $replace = array('-', '.html');

    $controller = str_replace($replace, "", $controller);
}
//can sua lai $aryURL[1] khi tach
if ($aryURL[1] != '') {

    $adminController = $aryURL[1];
}
//$controller = 'admin';
if (get_magic_quotes_gpc ()) {

    function stripslashes_deep($value) {

        $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
//CONTROLLER
if ($controller == 'quanly') {
    $objAdmin = new AdminController($aryConfigs, $aryConfigDB);
    $objAdmin->setParamsByUrl($aryURL);
    if (!$adminController) {
        $objAdmin->def();
    } else {
        if (method_exists($objAdmin, $adminController)) {
            $objAdmin->$adminController();
        } else {
            $msg = "Call to undefined method: " . get_class($objAdmin) . '->' . $adminController . '()';
            $objAdmin->writeLog($msg);
            $objAdmin->redirectURL($objAdmin->getRootPage());
        }
    }
} else {

    $matched = false;

    $objCrtl = new MyController($aryConfigs, $aryConfigDB);

    $objCrtl->setParamsByUrl($aryURL);

    if (!$controller) {

        $objCrtl->def();
        
    } else if ($controller && $controller != 'public') {

        foreach ($routers as $router) {

            if (preg_match("/$router/", $controller)) {

                if (method_exists($objCrtl, $router)) {

                    $matched = true;

                    $objCrtl->$router();

                    break;
                    
                } else {
                    $msg = "Call to undefined method: " . get_class($objCrtl) . '->' . $router . '()';
                    $objCrtl->writeLog($msg);
                    $objCrtl->redirectURL($objCrtl->getRootPage());
                }
            }
        }

        if($matched == false) {
            
            if (method_exists($objCrtl, $controller)) {

                $objCrtl->$controller();
                
            } else {
                $msg = "Call to undefined method: " . get_class($objCrtl) . '->' . $controller . '()';
                $objCrtl->writeLog($msg);
                $objCrtl->redirectURL($objCrtl->getRootPage());
            }
        }
    }
}
?>
